<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>4.画纹理</title>
</head>
<body>
	<canvas id="canvas"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		attribute vec4 a_position;
		attribute vec2 a_texcoord;
		
		varying vec2 v_texCoord;
		void main(){
			gl_Position = a_position;
			v_texCoord=a_texcoord;
		}
	</script>
	<script type="x-shader/x-fragment" id="fragment">
		precision mediump float;

		uniform sampler2D u_texture;
		varying vec2 v_texCoord;
		void main(){
			vec4 diffuseColor = texture2D(u_texture, v_texCoord);//纹理
			gl_FragColor = diffuseColor;
		}
	</script>
	<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/m4.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/primitives.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/texture-utils.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/chroma.min.js"></script>
	<script>
		/** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("canvas")
		const gl=canvas.getContext("webgl")
		canvas.width=window.innerWidth
		canvas.height=window.innerHeight
		gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

		const programInfo=webglUtils.createProgramInfo(gl,["vertex","fragment"])
		gl.useProgram(programInfo.program)
		
		const a_position=gl.getAttribLocation(programInfo.program,"a_position")
		const a_texCoord=gl.getAttribLocation(programInfo.program,"a_texcoord")

		const u_texture=gl.getUniformLocation(programInfo.program,"u_texture")
		const positions=[
			-1,1,1,1,//0
			1,1,1,1,//1
			1,-1,1,1,//2
			-1,-1,1,1,//3
		]
		const indexs=[
			0,1,2,
			0,2,3
		]
		const bufferPostition=gl.createBuffer()
		const bufferIndexs=gl.createBuffer()
		gl.bindBuffer(gl.ARRAY_BUFFER,bufferPostition)
		gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(positions), gl.STATIC_DRAW)
		
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,bufferIndexs)
		gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(indexs), gl.STATIC_DRAW)
		gl.enableVertexAttribArray(a_position)
		gl.vertexAttribPointer(a_position,4,gl.FLOAT,false,0,0)

		
		const t=[
			0,0,
			1,0,
			1,1,//左-右

			0,1,
			1,1,
			0,0,//右-左
		]
		
		const bT=gl.createBuffer()
		gl.bindBuffer(gl.ARRAY_BUFFER,bT)
		gl.enableVertexAttribArray(a_texCoord)

		gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(t), gl.STATIC_DRAW)
		gl.vertexAttribPointer(a_texCoord,2,gl.FLOAT,false,0,0)

		const texture = gl.createTexture();
		const image = new Image();
		// 图像加载完成后的回调函数
		image.onload = function() {
			// 将图像绑定到纹理对象
			
			gl.bindTexture(gl.TEXTURE_2D, texture);
			// 设置纹理参数
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
			
			// 将图像数据加载到纹理对象中
			gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
			gl.uniform1i(u_texture, 0);
			gl.drawElements(gl.TRIANGLES,6,gl.UNSIGNED_SHORT,0)
			setInterval(() => {
				positions[0]=positions[0]?0:1
		gl.bindBuffer(gl.ARRAY_BUFFER,bufferPostition)
		gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(positions), gl.STATIC_DRAW)
				gl.uniform1i(u_texture, 0);
				gl.drawElements(gl.TRIANGLES,6,gl.UNSIGNED_SHORT,0)
			}, 100);
		};
		// 设置图像的 URLage);
		image.src = '/webgl学习.png';
		// image.src = '/neg-x.jpg';
	</script>
</body>
</html>